Main site

Sample codes > Using product types

Using product types

As with any LLM AI model outputs, always verify the generated code for accuracy before deploying it live.

For the sample prompts here, your might get different responses since AI models have random elements.

AI Prompt:

I want a shipping calculator function.
Enrich the data and use the product type field in the items product object.
If the item product type is "furniture", multiply the kilogram weight of that item with 1.5 to get the shipping fee of that item.
If the item product type is "fashion", multiply the kilogram weight of that item with 0.2 to get the shipping fee of that item.

If the item is neither fashion or furniture, its shipping price is free.
Add the items shipping fees, and generate one shipping rate.

If the items include at least one furniture item, the delivery takes 1 to 2 weeks. Otherwise, the delivery takes 3 to 5 business days.
Make sure to convert the delivery times to delivery dates from now and correctly implement it.
Make sure to convert grams to kilograms.

AI Output:

import { enrichItemDetails } from "./modules.js";

export async function calculateShippingRates(DATA, env) {
  try {
    // Enrich the data with additional fields
    DATA = await enrichItemDetails(DATA);

    // Initialize the shipping rates
    let rates = [];

    // Calculate the shipping fee for each item
    let totalWeight = 0;
    let isFurniture = false;
    for (const item of DATA.items) {
      let weight = item.grams / 1000;
      totalWeight += weight;
      let shippingFee = 0;
      if (item.product.productType === "furniture") {
        isFurniture = true;
        shippingFee = weight * 1.5;
      } else if (item.product.productType === "fashion") {
        shippingFee = weight * 0.2;
      }
      item.shippingFee = shippingFee;
    }

    // Calculate the total shipping fee
    let totalShippingFee = 0;
    for (const item of DATA.items) {
      totalShippingFee += item.shippingFee;
    }

    // Set the delivery dates
    let minDeliveryDate = "";
    let maxDeliveryDate = "";
    if (isFurniture) {
      minDeliveryDate = new Date();
      minDeliveryDate.setDate(minDeliveryDate.getDate() + 7);
      maxDeliveryDate = new Date();
      maxDeliveryDate.setDate(maxDeliveryDate.getDate() + 14);
    } else {
      minDeliveryDate = new Date();
      minDeliveryDate.setDate(minDeliveryDate.getDate() + 3);
      maxDeliveryDate = new Date();
      maxDeliveryDate.setDate(maxDeliveryDate.getDate() + 5);
    }

    // Create the shipping rate
    rates.push({
      service_name: "Standard Shipping",
      service_code: "standard_shipping",
      total_price: totalShippingFee * 100,
      description: "Standard shipping",
      currency: DATA.currency,
      min_delivery_date: minDeliveryDate.toISOString(),
      max_delivery_date: maxDeliveryDate.toISOString(),
    });

    // Return the shipping rates
    return { rates };
  } catch (error) {
    // Return a rate with price equals to null in case of errors
    return { rates: [{ total_price: null }] };
  }
}